package com.nice.nice201110.job;

import com.nice.nice201110.entity.*;
import com.nice.nice201110.mapper.*;
import com.nice.nice201110.service.TnewService;
import com.nice.nice201110.utils.NiceTableEnum;
import org.quartz.JobExecutionContext;
import org.quartz.JobExecutionException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.quartz.QuartzJobBean;

import java.util.Date;
import java.util.LinkedHashMap;
import java.util.List;

public class WarningJob extends QuartzJobBean {

    @Autowired
    TBaseCapitalMapper tBaseCapitalMapper;

    @Autowired
    TBasePictureMapper tBasePictureMapper;

    @Autowired
    TnewService tnewService;

    @Autowired
    TLogMapper tLogMapper;

    @Autowired
    TUserMapper tUserMapper;

    @Autowired
    UtilDBMapper utilDBMapper;

    @Autowired
    TMedicationTreatmentMapper tMedicationTreatmentMapper;

    private TNews tNews = new TNews();

    @Override
    public void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {
        String param = (String) jobExecutionContext.getJobDetail().getJobDataMap().get("param");
        // 资产数量不足预警
        List<TBaseCapital> tBaseCapitalList = tBaseCapitalMapper.selectByWarning();

        if(tBaseCapitalList!=null&&tBaseCapitalList.size()>0){

            for(TBaseCapital tBaseCapital:tBaseCapitalList){
                String docid = String.valueOf(tBaseCapital.getId());
                List<TLog> tLogList = tLogMapper.selectByPrimaryKeys(docid, NiceTableEnum.form_base_capital,"0");
                int tOrganizationid = -1;
                if(tLogList!=null&&tLogList.size()>0){
                    for(TLog tLog:tLogList){
                        int userid = tLog.getUserid();
                        TUser tUser = tUserMapper.selectByPrimaryKey(userid);
                        if(tUser!=null){
                            tOrganizationid = tUser.getOrganizationid();
                            break;
                        }
                    }
                }
                tNews.setDate(new Date());
                tNews.setDataSources("定时任务");
                tNews.setRemark(tBaseCapital.toString());
                tNews.setTitle("资产["+tBaseCapital.getName()+"]数量不足，只有："+tBaseCapital.getNumber().toString());
                Boolean bl = tnewService.insert(tNews,tOrganizationid);
            }

        }
        // 药品数量不足预警
        List<TBasePicture> tBasePictureList = tBasePictureMapper.selectByWarning();
        if(tBasePictureList!=null&&tBasePictureList.size()>0){
            String sql = " select a.ID,a.NAME,u.ORGANIZATIONID from t_base_picture a   ";
            sql +=" inner join t_log l on a.ID = l.DOCID and l.TTABLE = 'base_capital' and TYPE = 0 ";
            sql +=" inner join t_user u on l.USERID = u.ID  ";
            sql +=" where a.NUMBER_WARNING <> 0 and a.NUMBER < a.NUMBER_WARNING  ";

            List<LinkedHashMap<String, Object>> list =  utilDBMapper.select(sql);
            for (int i=0;i<list.size();i++){
                LinkedHashMap<String,Object> linkedHashMap = list.get(i);
                 //  获取key字段大小写 根据别名来返回 key  如果是*  那么返回就是数据库字段（大小跟随数据库）
                String tOrganizationid = String.valueOf(linkedHashMap.get("ORGANIZATIONID"));
                String docid = String.valueOf(linkedHashMap.get("ID"));

                TBasePicture tBasePicture = tBasePictureMapper.selectByPrimaryKey(Integer.parseInt(docid));
                if(tBasePicture==null){continue;}
                tNews.setDate(new Date());
                tNews.setDataSources("定时任务");
                tNews.setRemark(tBasePicture.toString());
                tNews.setTitle("药品["+tBasePicture.getName()+"]数量不足，只有："+tBasePicture.getNumber().toString());
                Boolean bl = tnewService.insert(tNews,Integer.parseInt(tOrganizationid));
            }
        }
        // 疾病及用药处理
        List<TMedicationTreatment> tMedicationTreatmentList = tMedicationTreatmentMapper.selectByWarning();
        if(tMedicationTreatmentList!=null&&tMedicationTreatmentList.size()>0){
           String sql =" select distinct tu.ORGANIZATIONID,t.ID ";
            sql+=" from t_medication_treatment t  ";
            sql+=" inner join t_delivery_del dl on t.id = dl.PARENTID and dl.TYPE = '用药' ";
            sql+=" inner join t_log tl on t.id = tl.DOCID and tl.TYPE = 0 and tl.TTABLE = 'medication_treatment' ";
            sql+=" inner join t_user tu on tl.USERID = tu.id ";
            sql+=" where  DATE_FORMAT(date_add(t.PROCESSING_TIME, interval t.WARNING_DAY day),'%Y-%m-%d') = DATE_FORMAT(now(),'%Y-%m-%d') ";


            List<LinkedHashMap<String, Object>> list =  utilDBMapper.select(sql);
            for (int i=0;i<list.size();i++){
                LinkedHashMap<String,Object> linkedHashMap = list.get(i);
                String tOrganizationid = String.valueOf(linkedHashMap.get("ORGANIZATIONID"));
                String docid = String.valueOf(linkedHashMap.get("ID"));
                TMedicationTreatment tMedicationTreatment = tMedicationTreatmentMapper.selectByPrimaryKey(Integer.parseInt(docid));
                if(tMedicationTreatment==null){continue;}
                tNews.setDate(new Date());
                tNews.setDataSources("定时任务");
                tNews.setRemark(tMedicationTreatment.toString());
                tNews.setTitle("用药提醒，有需要重新用药..");
                Boolean bl = tnewService.insert(tNews,Integer.parseInt(tOrganizationid));

            }


        }

    }

}


